package org.intellij.markdown.parser

import org.intellij.markdown.flavours.commonmark.CommonMarkMarkerProcessor
import org.intellij.markdown.parser.constraints.MarkdownConstraints
import org.intellij.markdown.parser.constraints.MutableMarkdownConstraints
import org.intellij.markdown.parser.markerblocks.MarkerBlockProvider


class MutableMarkerProcessor(productionHolder: ProductionHolder,
                             startConstraints: MutableMarkdownConstraints,
                             val markerBlockProviderList: MutableList<MarkerBlockProvider<StateInfo>> = mutableListOf()
): CommonMarkMarkerProcessor(productionHolder, startConstraints) {

//    override var stateInfo: StateInfo = StateInfo(startConstraints,
//        startConstraints,
//        markersStack)

    override fun getMarkerBlockProviders(): List<MarkerBlockProvider<StateInfo>> = markerBlockProviderList

    override fun populateConstraintsTokens(pos: LookaheadText.Position,
                                           constraints: MarkdownConstraints,
                                           productionHolder: ProductionHolder) {
        if (constraints !is MutableMarkdownConstraints || !constraints.hasCheckbox()) {
            super.populateConstraintsTokens(pos, constraints, productionHolder)
            return
        }

        val line = pos.currentLine
        var offset = pos.offsetInCurrentLine
        while (offset < line.length && line[offset] != '[') {
            offset++
        }
        if (offset == line.length) {
            super.populateConstraintsTokens(pos, constraints, productionHolder)
            return
        }

        constraints.populateConstraintsTokens(pos,offset,productionHolder)

//        val type = when (constraints.types.lastOrNull()) {
//            '>' ->
//                MarkdownTokenTypes.BLOCK_QUOTE
//            '.', ')' ->
//                MarkdownTokenTypes.LIST_NUMBER
//            else ->
//                MarkdownTokenTypes.LIST_BULLET
//        }
//        val middleOffset = pos.offset - pos.offsetInCurrentLine + offset
//        val endOffset = min(pos.offset - pos.offsetInCurrentLine + constraints.getCharsEaten(pos.currentLine),
//            pos.nextLineOrEofOffset)
//
//        productionHolder.addProduction(listOf(
//            SequentialParser.Node(pos.offset..middleOffset, type),
//            SequentialParser.Node(middleOffset..endOffset, GFMTokenTypes.CHECK_BOX)
//        ))
    }

    class Factory(val markerBlockProviders: MutableList<MarkerBlockProvider<StateInfo>> = mutableListOf<MarkerBlockProvider<StateInfo>>()) : MarkerProcessorFactory {
        override fun createMarkerProcessor(productionHolder: ProductionHolder): MarkerProcessor<*> {
            return MutableMarkerProcessor(productionHolder, MutableMarkdownConstraints.BASE,markerBlockProviders)
        }
    }

}